package crypto

import (
	"github.com/pkg/errors"
)

//Encrypt 使用一个秘钥加密, 秘钥可以是对称秘钥，也可以是支持加解密的非对称秘钥或公钥
//如果参数key对应的算法不支持加密，则抛出UnsupportedAlgorithm（不支持的算法）异常
func Encrypt(key Key, source []byte) ([]byte, error) {
	encryptor, ok := key.(Encryptor)
	if ok {
		return encryptor.Encrypt(source)
	}
	return nil, errors.New("encrypt function is not provided by key")
}

//Decrypt 解密
func Decrypt(key Key, source []byte) ([]byte, error) {
	decryptor, ok := key.(Decryptor)
	if ok {
		return decryptor.Decrypt(source)
	}
	return nil, errors.New("encrypt function is not provided by key")
}
